package com.lambda.sample.swipeback;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lambda.widget.LSwipeBackLayout;
import com.lambda.widget.Utils;

public class LSwipeBackActivity extends AppCompatActivity implements LSwipeBackLayout.SwipeGestureDelegate {
    private LSwipeBackLayout mSwipeBackLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_back);

        Utils.convertFromTranslucent(this);

        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        ViewGroup decorView = (ViewGroup) window.getDecorView();
        decorView.setBackgroundDrawable(null);

        View contentView = decorView.getChildAt(0);
        contentView.setBackgroundResource(R.color.a_q);
        decorView.removeView(contentView);

        mSwipeBackLayout = (LSwipeBackLayout) LayoutInflater.from(this).inflate(R.layout.layout_lswipe_back, decorView, false);
        mSwipeBackLayout.setSwipeGestureDelegate(this);
        mSwipeBackLayout.addView(contentView);
        mSwipeBackLayout.setContentView(contentView);
        decorView.addView(mSwipeBackLayout);
    }

    @Override
    public void onBackPressed() {
        if (mSwipeBackLayout != null) {
            finish();
        } else {
            super.onBackPressed();
        }
    }

    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.close_exit);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onDrag() {

    }

    @Override
    public void onSwipeBack() {
        if (!isFinishing()) {
            finish();
            getWindow().getDecorView().setVisibility(View.GONE);
            overridePendingTransition(0, 0);
        }
    }
}
